package com.gm.controller;

import com.gm.dao.DepartmentDao;
import com.gm.dao.EmployeeDao;
import com.gm.entities.Department;
import com.gm.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@Controller
public class EmployeeController {
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private DepartmentDao departmentDao;

    @GetMapping("/emps")
    public String findAllEmps(Model model){
        Collection<Employee> employees = this.employeeDao.getAll();
        model.addAttribute("emps",employees);
        return "emp/list";
    }
    //点击来到增加页面
    @GetMapping("/emp")
    public String toAddPage(Model model){
       Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("depms",departments);
       return "emp/add";
    }
    //增加
    @PostMapping("/emp")
    public String addEmp(Employee employee){
//        System.out.println("得到的对象为"+employee);
        this.employeeDao.save(employee);
        return "redirect:/emps";
    }
    //点击来到编辑页面
    @GetMapping("/emp/{id}")
    public String toUpdPage(@PathVariable Integer id,
                            Model model){
//        查询员工信息
        Employee employee = this.employeeDao.get(id);
        model.addAttribute("emp",employee);
//        查询部门信息
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("depms",departments);
//        return "redirect:"
        return "emp/add";
    }
//      编辑
    @PutMapping("/emp")
    public String updEmp(Employee employee){
        this.employeeDao.save(employee);
        return "redirect:/emps";
    }
//      删除

    @DeleteMapping("/emp/{id}")
    public String delEmp(@PathVariable("id") Integer id){
        this.employeeDao.delete(id);
        return "redirect:/emps";
    }
//

}
